import Vue from 'vue'
import VueRouter from 'vue-router'
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}
Vue.use(VueRouter)

const routes = [
  {
    path: "/login",
    component: () => import("@/views/login")
  }, {
    path: "/index",
    component: () => import("@/views/index"),
    redirect: "/index/home",
    // 二级路由
    children: [{
      path: "home",
      component: () => import("@/views/home")
    }, {
      path: "articleList",
      component: () => import("@/views/articleList")
    }, {
      path: "addArticle",
      component: () => import("@/views/addArticle")
    }
    ]
  }, {
    path: "/",
    redirect: "/login"
  }
]

const router = new VueRouter({
  routes
})
// 白名单设置
let arr = ['/login', '/singIn', '/index',]
router.beforeEach((to, from, next) => {
  if (arr.indexOf(to.path) !== -1) {
    next()
  } else {
    if (localStorage.getItem("hm-back-token")) {
      next()
    } else {
      next("/login")
    }
  }

})
export default router
